package com.commerce.product.service;

/**
 * 防刷限流服务接口
 */
public interface AntiSpamService {

    /**
     * IP限流检查
     * @param ip IP地址
     * @return 是否通过限流检查
     */
    boolean checkIpRateLimit(String ip);

    /**
     * 用户限流检查
     * @param userId 用户ID
     * @return 是否通过限流检查
     */
    boolean checkUserRateLimit(Long userId);

    /**
     * 检查用户是否在黑名单中
     * @param userId 用户ID
     * @return 是否在黑名单中
     */
    boolean isUserBlacklisted(Long userId);

    /**
     * 检查IP是否在黑名单中
     * @param ip IP地址
     * @return 是否在黑名单中
     */
    boolean isIpBlacklisted(String ip);

    /**
     * 记录用户行为
     * @param userId 用户ID
     * @param ip IP地址
     * @param action 行为类型
     * @param userAgent 用户代理
     */
    void recordUserBehavior(Long userId, String ip, String action, String userAgent);

    /**
     * 分析用户行为并检测异常
     * @param userId 用户ID
     * @param ip IP地址
     * @return 是否为可疑行为
     */
    boolean analyzeSuspiciousBehavior(Long userId, String ip);

    /**
     * 将用户加入黑名单
     * @param userId 用户ID
     * @param reason 原因
     * @param duration 封禁时长（秒）
     */
    void addUserToBlacklist(Long userId, String reason, long duration);

    /**
     * 将IP加入黑名单
     * @param ip IP地址
     * @param reason 原因
     * @param duration 封禁时长（秒）
     */
    void addIpToBlacklist(String ip, String reason, long duration);

    /**
     * 从黑名单中移除用户
     * @param userId 用户ID
     */
    void removeUserFromBlacklist(Long userId);

    /**
     * 从黑名单中移除IP
     * @param ip IP地址
     */
    void removeIpFromBlacklist(String ip);

    /**
     * 综合检查（包括限流、黑名单、行为分析）
     * @param userId 用户ID
     * @param ip IP地址
     * @param userAgent 用户代理
     * @return 检查结果
     */
    AntiSpamResult comprehensiveCheck(Long userId, String ip, String userAgent);

    /**
     * 防刷检查结果
     */
    class AntiSpamResult {
        private boolean allowed;
        private String reason;
        private int remainingAttempts;
        private long retryAfter;

        public AntiSpamResult(boolean allowed, String reason) {
            this.allowed = allowed;
            this.reason = reason;
        }

        public AntiSpamResult(boolean allowed, String reason, int remainingAttempts, long retryAfter) {
            this.allowed = allowed;
            this.reason = reason;
            this.remainingAttempts = remainingAttempts;
            this.retryAfter = retryAfter;
        }

        // Getters and Setters
        public boolean isAllowed() { return allowed; }
        public void setAllowed(boolean allowed) { this.allowed = allowed; }
        public String getReason() { return reason; }
        public void setReason(String reason) { this.reason = reason; }
        public int getRemainingAttempts() { return remainingAttempts; }
        public void setRemainingAttempts(int remainingAttempts) { this.remainingAttempts = remainingAttempts; }
        public long getRetryAfter() { return retryAfter; }
        public void setRetryAfter(long retryAfter) { this.retryAfter = retryAfter; }
    }
}
